<template>
  <div class="">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="提现状态：">
          <el-select v-model="formInline.settlementStatus" placeholder="请选择">
            <el-option v-for="item in options" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易时间：">
          <el-date-picker :clearable="false" v-model="date" type="daterange" value-format="yyyy-MM-dd"
            range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺名称：">
          <el-input style="width: 320px" v-model="formInline.shopName" placeholder="请输入店铺名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="sumit_button" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" class="sumit_button" size="small" @click="toExamine">提现审核</el-button>
        <export-excel-button title="提现记录" action="/groupon/money/v1/withdraw/export" :params="ExportParams" />
      </div>
      <ex-table ref="table" :showSelect="true" @selectChange="selectChange" :fromTotalType="fromTotalType"
        :loading="loading" :table-column="tableColumn" :table-data="tableData" @pageSizeChange="handleSizeChange"
        @currentPageChange="handleCurrentChange">
      </ex-table>
    </div>
    <!-- 提现审核弹窗 -->
    <el-dialog title="提现审核" width="60%" :visible.sync="dialogTableVisible" :close-on-click-modal="false">
      <ex-table ref="tables" :loading="loading" :table-column="tableColumnWithdrawal" :table-data="tableDataWithdrawal"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange" :showPage="false">
        <template v-slot:operate="{ scope }">
          <el-input placeholder="请输入打款流水号" v-model="scope.row.relevancyNumber"></el-input>
        </template>
      </ex-table>
      <div class="dialog_buttom">
        <el-button type="danger" size="medium" style="margin-right:20px" @click="refuse" :loading="refuseLoading">拒绝打款
        </el-button>
        <el-button type="primary" size="medium" @click="sumitPayment" :loading="sumitLoading">确认打款</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  // components
  import {
    adminDict,
    grouponWithdrawLs,
    withdrawAgree,
    withdrawRefuse,
  } from "@/api/operateActive";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  import handlePageMixin from "@/mixins/handlePageMixin";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "billList",
    components: {
      ExportExcelButton,
    },
    mixins: [handlePageMixin],
    data() {
      return {
        loading: false,
        sumitLoading: false,
        refuseLoading: false,
        centerDialogVisible: false,
        formInline: {
          settlementStatus:"",//提现状态
          shopName: "", //店铺名称
          transactionStartTime: "", //交易起始时间
          transactionEndTime: "", //交易结束时间
        },
        dialogTableVisible: false, //提现审核弹窗
        date: [],
        detailInfo: {},
        tableData: [],
        options: [
          {id:'',value:'全部'},{id:'1',value:'处理中'},{id:'2',value:'打款成功'},{id:'3',value:'拒绝打款'},
        ],
        fromTotalType: "withdrawList",
        tableColumn: [{
            prop: "shopName",
            label: "店铺名称",
          },
          {
            prop: "billTypeDict",
            label: "交易类型",
            width: 155
          },
          {
            prop: "withdrawMoney",
            label: "提现金额"
          },
          {
            prop: "settlementStatusDict",
            label: "提现状态"
          },
          {
            prop: "relevancySerialNo",
            label: "交易流水号",
          },
          {
            prop: "transactionTime",
            label: "提现时间"
          },
        ],
        tableDataWithdrawal: [], //提现审核数组
        tableColumnWithdrawal: [{
            prop: "shopName",
            label: "店铺名称",
          },
          {
            prop: "billTypeDict",
            label: "交易类型"
          },
          {
            prop: "withdrawMoney",
            label: "提现金额",
          },
          {
            label: "打款流水号",
            slot: true,
            slotName: "operate",
            width: 240,
            fixed: "right",
          },
        ],
        storeList: [],
      };
    },
    computed: {
      ...mapGetters(["isChain"]),
      ExportParams() {
        const {
          id: ticketId
        } = this;
        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        return params;
      },
    },
    created() {},
    mounted() {
      // this.getCardtype();
      this.getList();
    },
    methods: {
      getCardtype() {
        //获取提现状态
        const params = {
          dictType: "merchant_groupon_bill_settlement_status",
        };
        adminDict(params).then((res) => {
          var arr = [{
            id: "",
            value: "全部",
          }, ];
          this.options = arr.concat(res.result[0].dictList);
        });
      },
      lookDetail(e) {
        console.log(e);
        this.$router.push({
          path: "/moneyManagement/bill-detail",
          query: {
            orderId: e.relevancyNo,
            type: e.billType,
          },
        });
      },
      search() {
        //搜索
        console.log(this.formInline);
        this.formInline.transactionStartTime = this.date[0];
        this.formInline.transactionEndTime = this.date[1];
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        // this.$refs.table.clearSelection()
        this.getList();
      },
      reset() {
        this.formInline = {};
        // this.formInline.fundFlow = ''
        // this.formInline.billType = ''
        this.date = [];
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.$refs.table.$refs.elTableBox.clearSelection()
        this.getList();
      },
      getList() {
        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        this.loading = true;
        grouponWithdrawLs(params)
          .then((res) => {
            this.tableData = res.result.records;
            this.tableData.forEach((item) => {
              item.withdrawMoney = '￥' + item.withdrawMoney
              if (item.relevancySerialNo == '') {
                item.relevancySerialNo = '-'
              }
              if (item.settlementStatus == 1) {
                item.settlementStatusDict = '处理中'
              } else if (item.settlementStatus == 2) {
                item.settlementStatusDict = '打款成功'
              } else if (item.settlementStatus == 3) {
                item.settlementStatusDict = '拒绝打款'
              } else {
                item.settlementStatusDict = '-'
              }
            });
            this.$refs.table.pageOptions.total = res.result.total;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      lookDetail(e) {
        this.centerDialogVisible = true;
      },
      selectChange(e) { //表格多选
        console.log(e)
        let list = e
        this.tableDataWithdrawal = e
      },
      toExamine() { //提现审核弹窗
        if (this.tableDataWithdrawal.length == 0) {
          this.$message({
            message: "请勾选提现选项",
            type: "error",
            duration: 1500,
          });
          return
        }
        this.tableDataWithdrawal.forEach((item) => {
          item.relevancyNumber == ''
        })
        this.dialogTableVisible = true
      },
      refuse() { //拒绝打款
        console.log(this.tableDataWithdrawal)
        let list = this.tableDataWithdrawal
        let arr = []
        list.forEach((item) => {
          var obj = {}
          obj.id = item.id
          obj.relevancySerialNo = ''
          arr.push(obj);
        })
        console.log('数组为', arr)
        this.refuseLoading = true
        withdrawRefuse(arr).then((res) => {
            this.$message({
              type: "success",
              message: "已拒绝",
            });
            this.reset()
            this.tableDataWithdrawal = []
            // this.$refs.table.clearSelection()
            this.dialogTableVisible = false
            this.refuseLoading = false
            this.$emit('success')
            this.reset()
          })
          .finally(() => {
            // this.reset()
            // this.$refs.table.clearSelection()
            this.tableDataWithdrawal = []
            this.dialogTableVisible = false;
            this.sumitLoading = false
          });
      },
      sumitPayment() { //确认打款
        let list = this.tableDataWithdrawal
        let arr = []
        let none = false
        list.forEach((item) => {
          if (item.relevancyNumber == '' || item.relevancyNumber==undefined) {
            none = true
          }
          var obj = {}
          obj.id = item.id
          obj.relevancySerialNo = item.relevancyNumber
          arr.push(obj);
        })
        if (none == true) {
          this.$message({
            message: "请填写打款流水号",
            type: "error",
            duration: 1500,
          });
          return
        }
        console.log('数组为', arr,list,list[0].relevancyNumber)
        this.sumitLoading = true
        withdrawAgree(arr).then((res) => {
            this.$message({
              type: "success",
              message: "确认成功",
            });
            this.tableDataWithdrawal = []
            // this.$refs.table.clearSelection()
            this.dialogTableVisible = false
            this.sumitLoading = false
            this.$emit('success')
            this.reset()
          })
          .finally(() => {
            // this.reset()
            this.tableDataWithdrawal = []
            // this.$refs.table.clearSelection()
            this.dialogTableVisible = false;
            this.sumitLoading = false
          });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-card-title {
    font-size: 20px;
  }

  .head {
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 20px;
  }

  .listBox {
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
  }

  .listBox_head {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
  }

  .sumit_button {
    color: #fff !important;
    background: #ff7b35 !important;
    border: 0;
  }

  .dialog_buttom {
    display: flex;
    justify-content: center;
    padding-top: 30px;
  }

</style>
